/**-----------------------------------------------------------------------------
-通用开发平台:用于客户端xml文件打包和解释
-作者：张金军
-所有人：张金军
-版权：版权所有，张金军，2005-2015，保留所有权利
-----------------------------------------------------------------------------*/

function XmlCompact(){

	/**
	 * 负责打包提交数据集
	 */
	this.xmlDoc          = new XmlDocument();
	this.isResult        = false;
	this.ispostData      = false;
	this.factionName     = "";
	this.fmethodName     = "";
	this.fserviceCode    = "";
	this.flogID          = "";
	/**
	 * 后台返回数据结果类型：
	 * 0:后台操作;1:后台数据集;2:后台返回自行处理数据
	 */
	this.resultType      = "0";
	this.dataValue       = "";
}
XmlCompact.prototype.setActionName = function(actionName)
{
	this.factionName  = actionName;
}
XmlCompact.prototype.setMethodName = function(methodName)
{
	this.fmethodName  = methodName;
}
XmlCompact.prototype.setLogID = function(logID)
{
	this.flogID  = logID;
}
XmlCompact.prototype.setServiceCode = function(serviceCode)
{
	this.fserviceCode  = serviceCode;
}

XmlCompact.prototype.getDataCompact = function(dataCompact)
{
	if (Yacon.isEmpty(this.factionName))
		this.factionName = window.className;
	
	var xmlCompact  = new XmlDocument();
	xmlCompact.loadXML("<root/>");
	var rootNode = xmlCompact.getRootNode();	
	var actionNode = xmlCompact.appendNode(rootNode,"action");
  	xmlCompact.appendAttribute("actionName",this.factionName.encode());
  	xmlCompact.appendAttribute("pagePath",window.pagePath.encode());
	xmlCompact.appendAttribute("methodName",this.fmethodName.encode());
	xmlCompact.appendAttribute("serviceCode",this.fserviceCode.encode());
	xmlCompact.appendAttribute("traceID",new Date().getTime());
	
	xmlCompact.appendCDATA(dataCompact);		
		
	if (window.parameter)
	{
		return "sessionID="+window.parameter["sessionID"]+"&switch_xml=<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+xmlCompact.getXml();
	}else
		return "switch_xml=<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+xmlCompact.getXml();
	
}
/**
 * 打包数据集公共属性
 * @param dataset 数据集
 * @return 根节点
 */
XmlCompact.prototype.getDataSet = function(dataset)
{
	this.xmlDoc.loadXML("<dataset/>");
	if (window.parameter)
	{
		for(var name in window.parameter)
		{
			if (!name.isEqual("logid"))
				dataset.setForeignField(name,window.parameter[name]);
		}
	}
	if (dataset.foreignRecord)
	{
		for (var j=0; j< dataset.foreignRecord.getFieldCount(); j++)
		{
			field = dataset.foreignRecord.getField(j);
			
			this.xmlDoc.appendAttribute(field.getFieldName(),field.getFieldValue().encode());
		}		
	}
	if (dataset.attributes)
	{
		for (var j=0; j< dataset.attributes.getFieldCount(); j++)
		{
			field = dataset.attributes.getField(j);	
			if (dataset.foreignRecord && dataset.foreignRecord.isExitField(field.getFieldName()))
			{}else
			{
				this.xmlDoc.appendAttribute(field.getFieldName(),field.getFieldValue().encode());
			}
		}			
	}	
	return this.xmlDoc.getRootNode();
}

/**
 * 打包数据集后台操作数据据
 * @param dataset 数据集
 * @return 打包后数据
 */
XmlCompact.prototype.getPost = function(dataset)
{
	 this.ispostData = false; 
	 var rootNode  = this.getDataSet(dataset);	 
	 var record    = dataset.firstElement;
	 while (record)
	 {
		 if (!record.getState().isEqual("past"))
		 {
			 var recordNode = this.xmlDoc.appendNode(rootNode,"record");
			 if (record.getState().isEqual("insert"))
				 this.xmlDoc.appendAttribute("type","1");
			 if (record.getState().isEqual("edit"))
	    		this.xmlDoc.appendAttribute("type","2");
			 if (record.getState().isEqual("delete"))
	    		this.xmlDoc.appendAttribute("type","3");
			    			
			var fieldNode,field;
			if (dataset.foreignRecord)
			{
				for (var j=0; j< dataset.foreignRecord.getFieldCount(); j++)
				{
					field = dataset.foreignRecord.getField(j);	
					fieldNode = this.xmlDoc.appendNode(recordNode,"field");
					this.xmlDoc.appendAttribute("name",field.getFieldName());
					this.xmlDoc.appendAttribute("value",field.getFieldValue().encode());
					this.xmlDoc.appendAttribute("oldvalue","");
				}		
			}
		
			for (var j=0; j< record.getFieldCount(); j++)
			{
				
				field = record.getField(j);	
				if (dataset.foreignRecord && dataset.foreignRecord.isExitField(field.getFieldName()))
				{}else
				{
					fieldNode = this.xmlDoc.appendNode(recordNode,"field");
					this.xmlDoc.appendAttribute("name",field.getFieldName());
					this.xmlDoc.appendAttribute("value",field.getFieldValue().encode());
					this.xmlDoc.appendAttribute("oldvalue",field.getFieldOldValue().encode());
				}
			}
			this.ispostData = true;
		}
		record = record.nextRecord();
	}
	return this.getDataCompact(this.xmlDoc.getXml());	
}
/**
 * 前后台数据交换
 * @param dataset 数据集
 * @return 打包数据
 */
XmlCompact.prototype.getSwitch = function(dataset)
{
	 var rootNode = this.getDataSet(dataset);	
	 if (dataset.whereRecord ||dataset.foreignRecord)
	 {
		var recordNode = this.xmlDoc.appendNode(rootNode,"record");
		this.xmlDoc.appendAttribute("type","0");
		
		var record = dataset.foreignRecord;	
		var fieldNode,field;
		if (record)
		{
			for (var j=0; j< record.getFieldCount(); j++)
			{
				field = record.getField(j);	
				fieldNode = this.xmlDoc.appendNode(recordNode,"field");
				this.xmlDoc.appendAttribute("name",field.getFieldName());
				this.xmlDoc.appendAttribute("value",field.getFieldValue().encode());
				this.xmlDoc.appendAttribute("oldvalue","");
			}		
		}
		var record = dataset.whereRecord;
		if (record)
		{
			for (var j=0; j< record.getFieldCount(); j++)
			{
				field = record.getField(j);	
				if (dataset.foreignRecord && dataset.foreignRecord.isExitField(field.getFieldName()))
				{}else
				{
					fieldNode = this.xmlDoc.appendNode(recordNode,"field");
					this.xmlDoc.appendAttribute("name",field.getFieldName());
					this.xmlDoc.appendAttribute("value",field.getFieldValue().encode());
					this.xmlDoc.appendAttribute("oldvalue","");
				}
			}
		}
	}
	var record    = dataset.firstElement;
	 while (record)
	 {
		 var recordNode = this.xmlDoc.appendNode(rootNode,"record");
		 this.xmlDoc.appendAttribute("type","0");
			
		 for (var j=0; j< record.getFieldCount(); j++)
		{
			
			field = record.getField(j);	
			fieldNode = this.xmlDoc.appendNode(recordNode,"field");
			this.xmlDoc.appendAttribute("name",field.getFieldName());
			this.xmlDoc.appendAttribute("value",field.getFieldValue().encode());
			this.xmlDoc.appendAttribute("oldvalue","");
		}
		this.ispostData = true;
		record = record.nextRecord();
	}
	/* var record    = dataset.firstElement;
	 while (record)
	 {
		 var recordNode = this.xmlDoc.appendNode(rootNode,"record");
		 this.xmlDoc.appendAttribute("type","0");
			
		 for (var j=0; j< record.getFieldCount(); j++)
		{
			
			field = record.getField(j);	
			fieldNode = this.xmlDoc.appendNode(recordNode,"field");
			this.xmlDoc.appendAttribute("name",field.getFieldName());
			this.xmlDoc.appendAttribute("value",field.getFieldValue().encode());
			this.xmlDoc.appendAttribute("oldvalue","");
		}
		this.ispostData = true;
		record = record.nextRecord();
	}*/
	 
	return this.getDataCompact(this.xmlDoc.getXml());	
} 

/**
 * 获取已打包数据
 * @return
 */
XmlCompact.prototype.getData=function()
{
	return this.xmlDoc.getXml();
}

/**
 * 校验从后台接收得到的数据
 * @param xmlStr 所接收数据
 * @return
 */
XmlCompact.prototype.analyzeData = function(xmlStr)
{
	
	if (window.log.allControl&&window.log.xmlRecv)
	{
		alert("XmlCompact:analyzeData:"+xmlStr);
	}
	if (xmlStr.startsWith("alert"))
	{
		eval(xmlStr);
		return;
	}
	this.isResult = false;
	this.isDataResult = false;
	if(xmlStr.isEqual("false")||xmlStr.isEqual(""))
	{
		return false;
	}

	this.xmlDoc.loadXML(xmlStr);
	if (this.xmlDoc.getRootNode()== null)
	{
		alert("从后台获取的XML数据解析报错！");
		return false;
	}
	
	var rootNode = this.xmlDoc.getRootNode().getElementsByTagName("action")[0];	
	if (rootNode == null)
	{
		alert("从后台没有获取到业务数据！");
		return false;
	}
	
	this.dataValue = "";
	
	if (rootNode.getAttribute("type").isEqual("1"))
	{
		this.resultType  ="1";
		if (rootNode.getAttribute("result").isEqual("0000"))
		{
			this.dataValue = rootNode.childNodes[0].nodeValue;
			this.isResult = true;
			return true;
		}
		else
		{
			alert(rootNode.text);
			this.isResult = false;
			return false;
		}
	}else if (rootNode.getAttribute("type").isEqual("0"))
	{
		this.resultType  ="0";
		if (rootNode.getAttribute("result").isEqual("0000"))
		{
			if (!Yacon.isEmpty(rootNode.childNodes[0].nodeValue))
				alert(rootNode.childNodes[0].nodeValue);
			this.isResult = true;
			
			return true;
		}
		else
		{
			if (!Yacon.isEmpty(rootNode.childNodes[0].nodeValue))
				alert(rootNode.childNodes[0].nodeValue);
			this.isResult = false;
			return false;
		}
	}else if (rootNode.getAttribute("type").isEqual("2"))
	{
		this.resultType  ="2";
		this.dataValue = rootNode.childNodes[0].nodeValue;
		this.isResult = true;
		return true;
	}	
}

/**
 * 初始化数据集
 * @param dataset 需要初始化的数据集
 * @return
 */
XmlCompact.prototype.loadDataSet= function(dataset)
{
	if (!Yacon.isObject(dataset)||this.dataValue.isEqual(""))
		return false;
	
	dataset.free();
	/**
	 * 数据返回类型
	 */
	dataset.resultType = this.resultType;	
	if (this.resultType =="1")
	{
		this._initDataSet(this.dataValue,dataset);
	}else
	{
		dataset.dataValue  = this.dataValue;
	}
	
	return true;
}


/**
 * 填充数据集
 * @param dataset 需要填充的数据集
 * @return
 */
XmlCompact.prototype.fillDataSet= function(dataset)
{
		
	if (!Yacon.isObject(dataset)||this.dataValue.isEqual(""))
		return false;
	/**
	 * 数据返回类型
	 */
	dataset.resultType = this.resultType;
	
	if (this.resultType =="1")
	{
		this._initDataSet(this.dataValue,dataset);
	}else
	{
		dataset.dataValue  = this.dataValue;
	}
	
	return true;
}

/**
 * 填充数据集
 * @param dataset 需要填充的数据集
 * @return
 */
XmlCompact.prototype._initDataSet= function(xmldate,dataset)
{
	if (window.log.allControl&&window.log.xmlLoadData)
	{
		alert("XmlCompact:_initDataSet:"+xmldate);
	}
	
	this.xmlDoc.loadXML(xmldate);
	var dataSetNode = this.xmlDoc.getRootNode();
	
	for (var j=0; j< dataSetNode.attributes.length; j++)
	{
		fieldNode = dataSetNode.attributes[j];	
		dataset.addAttribute(fieldNode.nodeName,fieldNode.nodeValue);
	}
		
	/**
	 * 添加表头字段
	 */
	/*var columnNode;
	var columnSNode =  dataSetNode.childNodes[0];	
	for (var i=0; i< columnSNode.childNodes.length; i++)
	{
		columnNode = columnSNode.childNodes[i];
		
		dataset.createTitleField(columnNode.getAttribute("fieldName"),columnNode.getAttribute("fieldLabel"));
	}	*/	
	/**
	 * 添加记录
	 */
	var recordSNode =  dataSetNode.childNodes[0];
	var fieldNode,fieldSNode,recordNode;
	for (var i=0; i< recordSNode.childNodes.length; i++)
	{
		recordNode = recordSNode.childNodes[i];		
		dataset.createPureRecord("past");
		fieldSNode =recordNode.childNodes[0];
		for (var j=0; j< fieldSNode.childNodes.length; j++)
		{
			fieldNode = fieldSNode.childNodes[j];	
			
			dataset.setFieldValueText(fieldNode.getAttribute("name"),fieldNode.getAttribute("value"),fieldNode.getAttribute("text"));
		}
		if(recordNode.childNodes[1] && recordNode.childNodes[1].childNodes[0])
		{
			this._initDataSet(this.xmlDoc.getXml(recordNode.childNodes[1].childNodes[0]), dataset.getRecord().createChildDataSet());
		}
		
	}
	
	dataset.beforeFirst();
	
}

